package com.hfengxiang.example.canvasrotatedemo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;

import java.time.temporal.ValueRange;

public class AnimationActivity extends AppCompatActivity implements View.OnClickListener {

    private FrameLayout mFlContainer;
    private ImageView mIvTarget;
    private Button mBtnAnimate;
    private ValueAnimator animator;
    private Rect starRect;
    private Rect targetRect;
    private AnimateView animateView;
    private float startRotation = 0;
    private float endRotation = 360;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        initView();
        animator = ValueAnimator.ofFloat(0,1);
        animator.setDuration(3000);
        animator.setInterpolator(new FastOutSlowInInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float fraction = animation.getAnimatedFraction();
                Rect currentRect = AnimateUtils.getCurrentRect(starRect, targetRect, fraction);
                animateView.setRotation(startRotation+(endRotation-startRotation)*fraction);
                animateView.setRenderRect(currentRect);
                animateView.invalidate();

            }
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });

    }

    private void initView() {
        mFlContainer = (FrameLayout) findViewById(R.id.fl_container);
        mIvTarget = (ImageView) findViewById(R.id.iv_target);
        mBtnAnimate = (Button) findViewById(R.id.btn_animate);
        mBtnAnimate.setOnClickListener(this);
        animateView = new AnimateView(this);
        animateView.setBitmap(Utils.getPhoto(getResources(), getResources().getDisplayMetrics().widthPixels));
        animateView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        mFlContainer.addView(animateView);
        starRect = new Rect();
        int measuredWidth = getResources().getDisplayMetrics().widthPixels;
        int measuredHeight = getResources().getDisplayMetrics().heightPixels;
        starRect.set(measuredWidth/3,measuredHeight/2,measuredWidth*2/3,measuredHeight);
    }

    @Override
    public void onClick(View v) {
        targetRect = new Rect();
        mIvTarget.getHitRect(targetRect);
        animateView.setRotation(90);
        if(animator.isRunning())return;
        animator.start();
    }
}